package com.sugon.zip

import java.io.IOException
import java.net.URI

import org.apache.hadoop.conf.Configuration


import org.apache.hadoop.fs._

object HdfsUtil {

  val conf: Configuration = new Configuration()
  conf.set("fs.defaultFS", "seaweedfs://slave11:8888")
  conf.set("fs.seaweedfs.impl", "seaweed.hdfs.SeaweedFileSystem")
  conf.set("fs.AbstractFileSystem.seaweedfs.impl", "seaweed.hdfs.SeaweedAbstractFileSystem")
  var fs: FileSystem = _
  var files: RemoteIterator[LocatedFileStatus] = _

  def getFileSystem: FileSystem = {
    if (fs == null) {
      fs = FileSystem.get(conf)
    }
    fs
  }

  def getFiles(HDFSPath: String): RemoteIterator[LocatedFileStatus] = {
    try {
      fs = FileSystem.get(new URI(HDFSPath), conf)
    } catch {
      case e: IOException => {
        e.printStackTrace
      }
    }
    files
  }

  def getFiles(HDFSPath: String, targetPath: String): RemoteIterator[LocatedFileStatus] = {
    try {
      fs = FileSystem.get(new URI(HDFSPath), conf)
      // 返回指定路径下所有的文件
      files = fs.listFiles(new Path(targetPath), false)
    } catch {
      case e: IOException => {
        e.printStackTrace
      }
    }
    files
  }

  def mkdir(finalPath: String): FSDataOutputStream = {
    fs.create(new Path(finalPath))
  }

  def rename(oldPath: String, finalPath: String): Boolean = {
    fs.rename(new Path(oldPath), new Path(finalPath))
  }

  def exist(existPath: String): Boolean = {
    fs.exists(new Path(existPath))
  }

  def delete(deletePath: String): Boolean = {
    fs.delete(new Path(deletePath), true)
  }

  def read(readPath: String): FSDataInputStream = {
    fs.open(new Path(readPath))
  }

  def getAllFiles(path: String): Array[FileStatus] = {
    val fs: FileSystem = FileSystem.get(URI.create(path), conf)
    val files: Array[FileStatus] = fs.listStatus(new Path(path))
    for (file <- files) {
      println(file.getPath.getName)
      println(file.getPath.toString)
    }
    files

  }

  def main(args: Array[String]): Unit = {
    getAllFiles("hdfs://10.10.4.1:8020/ibc/datalogs/apachelogs/archive/2018")
  }

  def close() = {
    try {
      if (fs != null) {
        fs.close()
      }
    } catch {
      case e: IOException => {
        e.printStackTrace
      }
    }
  }


}